package com.ktjy.sample.aop;

import org.aspectj.lang.JoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

public class LoggerAspect {
    private static final Logger logger= LoggerFactory.getLogger(LoggerAspect.class);
    /**
     * 前置增强-打印进入方法和参数
     */
    public void before(JoinPoint jp){
        logger.info("[LoggerAspect 前置增强] 正在调用{}的{}方法. 方法入参:{}",
                jp.getTarget(),jp.getSignature().getName(),
                Arrays.toString(jp.getArgs()));
    }

    /**
     * 后置增强-打印返回结果
     */
    public void afterReturning(JoinPoint jp,Object result){
        logger.info("[LoggerAspect 后置增强] {}的{}方法正常返回. 返回结果:{}",
                jp.getTarget(),jp.getSignature().getName(),
                result);
    }
    /**
     * 异常抛出增强-执行事物回滚
     */
    public void afterThrowing(JoinPoint jp,Throwable e){
        logger.info("[TransactionAspect异常抛出增强]模拟业务异常回滚事物,异常信息为:"+e.getMessage());
        logger.info("session.rollback()");
    }
    /**
     * 最终增强-执行关闭数据库资源
     */
    public void after(JoinPoint jp){
        logger.info("[LoggerAspect 最终增强]模拟业务完成释放资源.... ");
        logger.info("MyBatisUtil.closeSqlSession(sqlSession);");
    }
}